﻿using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using FranceTriple.Dal;
using System.Linq;

namespace TeamFranceTriple
{
    public partial class EventList : BaseUserControl
    {
        public DateTime SelectedDate { get; set; }
        private Int32 DefaultEventCategoryId;
        private DateTime DefaultEventStartDate;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (Request.Form["ctl00$Calendar1$EventCategoryRadioButtonList"] != null)
                DefaultEventCategoryId = Int32.Parse(Request.Form["ctl00$Calendar1$EventCategoryRadioButtonList"]);

            var CalendarControl = (Calendar)ThisMaster.FindControl("Calendar1");
            var EventCalendar = (System.Web.UI.WebControls.Calendar)CalendarControl.FindControl("EventCalendar1");
            if (EventCalendar != null)
                DefaultEventStartDate = EventCalendar.SelectedDate;

            if (ThisMaster.Requester.Roles.Contains("RoleEveningChief"))
                EventCategoryPanel.Visible = true;
        }

        protected void EventListView_DataBound(object sender, EventArgs e)
        {
            var author = (HiddenField)EventListView.InsertItem.FindControl("UserNameAuthorEventHidden");
            var authorLabel = (Label)EventListView.InsertItem.FindControl("UserNameAuthorEventLabel");

            var InsertButtonInsertItem = (Button)EventListView.InsertItem.FindControl("InsertButton");
            var CancelButtonInsertItem = (Button)EventListView.InsertItem.FindControl("CancelButton");

            var TitleEventTextBox = (TextBox)EventListView.InsertItem.FindControl("TitleEventTextBox");
            var DateStartEventTextBox = (TextBox)EventListView.InsertItem.FindControl("DateStartEventTextBox");
            var DateEndEventTextBox = (TextBox)EventListView.InsertItem.FindControl("DateEndEventTextBox");
            var Image1 = (Image)EventListView.InsertItem.FindControl("Image1");
            var Image2 = (Image)EventListView.InsertItem.FindControl("Image2");
            var AllDayEventCheckBox = (CheckBox)EventListView.InsertItem.FindControl("AllDayEventCheckBox");
            var DescriptionEventTextBox = (TextBox)EventListView.InsertItem.FindControl("DescriptionEventTextBox");
            var EventCategoryDropDownList = (DropDownList)EventListView.InsertItem.FindControl("EventCategoryDropDownList");

            if (ThisMaster.Requester.Roles.Contains("RoleEveningChief"))
            {
                author.Value = ThisMaster.Requester.Profile.UserName;
                authorLabel.Text = ThisMaster.Requester.Profile.UserName;

                InsertButtonInsertItem.Visible = true;
                CancelButtonInsertItem.Visible = true;
                TitleEventTextBox.Visible = true;
                DateStartEventTextBox.Visible = true;
                DateEndEventTextBox.Visible = true;
                AllDayEventCheckBox.Visible = true;
                DescriptionEventTextBox.Visible = true;
                EventCategoryDropDownList.Visible = true;
                Image1.Visible = true;
                Image2.Visible = true;
                if (DefaultEventCategoryId > 0)
                    EventCategoryDropDownList.SelectedValue = DefaultEventCategoryId.ToString();
                if (DefaultEventStartDate != new DateTime())
                    DateStartEventTextBox.Text = DefaultEventStartDate.ToShortDateString();
            }
            else
            {
                InsertButtonInsertItem.Visible = false;
                CancelButtonInsertItem.Visible = false;
                TitleEventTextBox.Visible = false;
                DateStartEventTextBox.Visible = false;
                DateEndEventTextBox.Visible = false;
                AllDayEventCheckBox.Visible = false;
                DescriptionEventTextBox.Visible = false;
                EventCategoryDropDownList.Visible = false;
                Image1.Visible = false;
                Image2.Visible = false;
            }
        }

        protected void EventListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                var  AuthorEventLabel = e.Item.FindControl("AuthorEventLabel") as Label;

                if (AuthorEventLabel.Text.ToLower() == ThisMaster.Requester.Profile.UserName.ToLower())
                {
                    Button btn = e.Item.FindControl("DeleteButton") as Button;
                    Button btn2 = e.Item.FindControl("EditButton") as Button;

                    if (btn != null)
                        btn.Visible = true;
                    if (btn2 != null)
                        btn2.Visible = true;
                }
            }
        }

        protected void EventListView_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void EventListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
            //var category = (HiddenField)EventListView.EditItem.FindControl("EventCategoryHidden");
            var ddl = (DropDownList)EventListView.EditItem.FindControl("EventCategoryDropDownList");
            if (ddl != null)
            {
                var eventCategory = EventDal.GetEventCategory(Int32.Parse(ddl.SelectedValue));

                EventDal.UpdateEntity(new Event()
                {
                    Id = Int32.Parse(e.NewValues["Id"].ToString()),
                    TitleEvent = e.NewValues["TitleEvent"].ToString(),
                    AuthorEvent = e.NewValues["AuthorEvent"].ToString(),
                    DateStartEvent = DateTime.Parse(e.NewValues["DateStartEvent"].ToString()),
                    DateEndEvent = DateTime.Parse(e.NewValues["DateEndEvent"].ToString()),
                    AllDayEvent = Boolean.Parse(e.NewValues["AllDayEvent"].ToString()),
                    DescriptionEvent = e.NewValues["DescriptionEvent"].ToString(),
                    EventCategory = eventCategory
                });
            }

            //   e.NewValues["EventCategory"] = EventDal.GetEventCategory(Int32.Parse(ddl.SelectedValue));
            //var updatingEvent = (Event)EventListView  //.DataSourceObject; //.Items[e.ItemIndex].DataItem;
        }

        protected void EventListView_ItemDeleting(object sender, ListViewDeleteEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }            
        }

        protected void EventCategoryListView_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }
        protected void EventCategoryListView_ItemInserted(object sender, ListViewInsertedEventArgs e)
        {
            RefreshCategoryCombo();
        }

        protected void EventCategoryListView_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            if (!this.Page.IsValid)
            {
                e.Cancel = true;
                return;
            }
        }

        protected void EventCategoryListView_ItemDeleted(object sender, ListViewDeletedEventArgs e)
        {
            RefreshCategoryCombo();
        }

        private void RefreshCategoryCombo()
        {
            // Mise à jour des combos de la liste des evenements
            EventListView.DataBind();
            EventListUpdatePanel.Update();

            // Mise à jour de la liste des catégories sur le calendrier de la master page
            var CalendarControl = (Calendar)ThisMaster.FindControl("Calendar1");

            var EventCategoryRadioButtonList = (RadioButtonList)CalendarControl.FindControl("EventCategoryRadioButtonList");
            if (EventCategoryRadioButtonList != null)
                EventCategoryRadioButtonList.DataBind();

            var CalendarUpdatePanel = (UpdatePanel)CalendarControl.FindControl("CalendarUpdatePanel");
            if (CalendarUpdatePanel != null)
                CalendarUpdatePanel.Update();

        }
    }
}